package com.EAsystem.entity;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

public class Homework {
    private Long id;
    private Course course;
    private String title;
    private String content;
    private LocalDateTime deadline;
    private LocalDateTime createTime;
    private LocalDateTime updateTime;
    private List<HomeworkSubmission> homeworkSubmissionList;

    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Course getCourse() {
        return course;
    }

    public void setCourse(Course course) {
        this.course = course;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public LocalDateTime getDeadline() {
        return deadline;
    }

    public void setDeadline(LocalDateTime deadline) {
        this.deadline = deadline;
    }


    public void setDeadline(String deadlineStr) {
        if (deadlineStr != null && !deadlineStr.trim().isEmpty()) {
            try {
                if (deadlineStr.length() == 16) { // yyyy-MM-ddTHH:mm 格式
                    this.deadline = LocalDateTime.parse(deadlineStr.replace("T", " ") + ":00", formatter);
                } else if (deadlineStr.length() == 19) { // yyyy-MM-dd HH:mm:ss 格式
                    this.deadline = LocalDateTime.parse(deadlineStr, formatter);
                } else {
                    this.deadline = LocalDateTime.parse(deadlineStr.replace("T", " "), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
                }
            } catch (Exception e) {
                System.err.println("日期解析失败: " + deadlineStr + ", 错误: " + e.getMessage());
                this.deadline = null;
            }
        }
    }

    public String getDeadlineStr() {
        return deadline != null ? deadline.format(formatter) : null;
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    public String getCreateTimeStr() {
        return createTime != null ? createTime.format(formatter) : null;
    }

    public LocalDateTime getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(LocalDateTime updateTime) {
        this.updateTime = updateTime;
    }

    public String getUpdateTimeStr() {
        return updateTime != null ? updateTime.format(formatter) : null;
    }

    public List<HomeworkSubmission> getHomeworkSubmissionList() {
        return homeworkSubmissionList;
    }

    public void setHomeworkSubmissionList(List<HomeworkSubmission> homeworkSubmissionList) {
        this.homeworkSubmissionList = homeworkSubmissionList;
    }
}
